{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When creating exoeriment with ```make_experiment```, we can respecify evaluation metric by the parameter ```reward_metric```, as shown in the following example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from hyperts.experiment import make_experiment\n",
    "\n",
    "experiment = make_experiment(train_data, \n",
    "                             task='forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             reward_metric='mae',\n",
    "                             ...) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to passing in bulit-in supported metrics, we can alse customize metrics to meet the requirements of a specific scene. For example:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### methed 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "def custom_metric(y_true, y_pred, epsihon=1e-06):\n",
    "    if (y_true < 0).any():\n",
    "        y_true = np.clip(y_true, a_min=epsihon, a_max=abs(y_true))\n",
    "\n",
    "    if (y_pred < 0).any():\n",
    "        y_pred = np.clip(y_pred, a_min=epsihon, a_max=abs(y_pred))\n",
    "\n",
    "    return mean_squared_error(np.log1p(y_true), np.log1p(y_pred))\n",
    "\n",
    "\n",
    "experiment = make_experiment(train_data, \n",
    "                             task='forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             reward_metric=custom_metric,\n",
    "                             optimize_direction='min',\n",
    "                             ...)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note:** In this way, the ```optimize_direction``` must be specified."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### methed 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error, make_scorer\n",
    "\n",
    "def custom_metric(y_true, y_pred, epsihon=1e-6):\n",
    "    if (y_true < 0).any():\n",
    "        y_true = np.clip(y_true, a_min=epsihon, a_max=abs(y_true))\n",
    "\n",
    "    if (y_pred < 0).any():\n",
    "        y_pred = np.clip(y_pred, a_min=epsihon, a_max=abs(y_pred))\n",
    "\n",
    "    return mean_squared_error(np.log1p(y_true), np.log1p(y_pred))\n",
    "\n",
    "custom_scorer = make_scorer(custom_metric, greater_is_better=True, needs_proba=False)\n",
    "\n",
    "experiment = make_experiment(train_data, \n",
    "                             task='forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             reward_metric=custom_metric,\n",
    "                             scorer=make_scorer,\n",
    "                             ...) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note:** In this way, the ```scorer``` must be specified."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}